package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {

    private UserService userService = new UserServiceImpl();
    @RequestMapping("/login_page")
    public String showLoginPage() {
        return "Login"; // 返回Login.jsp页面
    }

    @GetMapping("/login")
    public void returnLoginPage(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        String sqlpassword = userService.getUserPassword(name);
        if (sqlpassword.equals(password))
        {response.getWriter().write("user_not_found");}
        else if (password.equals(sqlpassword)) {response.getWriter().write("success");
        session.setAttribute("username",name);
        }
        else response.getWriter().write("incorrect_password");
    }


    @RequestMapping("/main")
    public String showMainPage() {
        return "main"; // 返回main.jsp页面
    }

    @GetMapping("/forgot-password")
    public String showForgotPasswordPage() {
        return "Password_Recovery"; // 返回视图名称，Spring MVC会解析为对应的JSP页面
    }


}
